package com.ustcinfo.study.hbase.wangzhigang;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import org.apache.hadoop.hbase.client.Put;

public class GetMySqlData {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/test";
    static final String USER = "root";
    static final String PASS = "123";
     
    public  static List<Put> putMyList(){
    	Connection conn=null;
    	Statement stmt=null;
    	List<Put> list=null;
    	
        try {
			Class.forName("com.mysql.jdbc.Driver");		
			conn = DriverManager.getConnection(DB_URL,USER,PASS);		
			stmt = conn.createStatement();	
			String sql = "SELECT * FROM transfer_task_to";		
			ResultSet rs = stmt.executeQuery(sql);
			list=new LinkedList<Put>();

            while(rs.next()){
                Put put = new Put(rs.getBytes("id"));  //row key
                put.addColumn("F1".getBytes(), "task_id".getBytes(), rs.getBytes("task_id"));  //簇 列 值
                put.addColumn("F1".getBytes(), "create_time".getBytes(), rs.getBytes("create_time"));
                put.addColumn("F1".getBytes(), "start_time".getBytes(), rs.getBytes("start_time"));
                put.addColumn("F1".getBytes(), "out_filter_number".getBytes(), rs.getBytes("out_filter_number"));
                put.addColumn("F1".getBytes(), "from_run_param".getBytes(), rs.getBytes("from_run_param"));
                
                put.addColumn("F2".getBytes(), "current_retry_times".getBytes(), rs.getBytes("current_retry_times"));
                put.addColumn("F2".getBytes(), "retry_flag".getBytes(), rs.getBytes("retry_flag"));
                put.addColumn("F2".getBytes(), "parent_task_id".getBytes(), rs.getBytes("parent_task_id"));
                put.addColumn("F2".getBytes(), "end_time".getBytes(), rs.getBytes("end_time"));
                put.addColumn("F2".getBytes(), "kill_time".getBytes(), rs.getBytes("kill_time"));
                put.addColumn("F2".getBytes(), "failure_time".getBytes(), rs.getBytes("failure_time"));
                
                list.add(put);    	
            }
                  
            rs.close();
			
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		} catch (SQLException e) {
			
			e.printStackTrace();
		} finally{
            try{
                if(stmt!=null) 
                	stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) 
                	conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
		}
        
		return list;      
    }
    
    public static void main(String[] args){
        
    	if(HBaseMetaDao.tableExist("BD_TEST")){
    		HBaseMetaDao.deleteTable("BD_TEST");
        	System.out.println("exist  BD_TEST,already delete...");
        }

    	HBaseMetaDao.createTable("BD_TEST","F1","F2");
        
        if(HBaseMetaDao.tableExist("BD_TEST")){
        	System.out.println("BD_TEST create success...");

        	HBaseOperationDao.putList("BD_TEST",putMyList());
        }
    }
}
